<?php

declare(strict_types=1);

namespace app\controller\sys;

// 引入框架内置类
use think\facade\View;

use app\utils\ReUtils;
use app\model\Category as CategoryModel;
use app\model\ArticleEn as ArticleModel;
// use app\model\Tag as TagModel; 

/**
 * +----------------------------------------------------------------------
 * 后台文章控制制器
 * @author huwhis@163.com
 * @version   0.0.1
 * +----------------------------------------------------------------------
 */
class ArticleEn extends Base
{
    protected $modelName = "ArticleEn";

    public function index()
    {
        $categories = CategoryModel::getArticleList();

        $cid = $this->app->request->param('cid') ?: 0;

        $params = $this->app->request->param();

        $list = ArticleModel::queryPage($params);

        View::assign(
            [
                'categories' => $categories,
                'list' => $list,
                'cid' => $cid,
                'key' => isset($params['key']) ? (string)$params['key'] : ''
            ]
        );

        return View::fetch();
    }

    public function save($id = 0, $cid = 0)
    {
        if ($this->app->request->isPost()) {
            $params = $this->app->request->param();

            if (!$params['cid']) {
                $this->error('请选择栏目');
            }

            if ($params['title'] == '') {
                $this->error("标题不能为空");
            }

            $params['content'] =  isset($params['content']) ? $params['content'] : '';
            $params['keywords'] = str_replace(' ', '', $params['keywords']);

            $id = $params['id'];
            unset($params['id']);

            try {
                if ($id != 0) {
                    // $oldkey = ArticleModel::where('id', $id)->value('keywords');

                    // $oldkeys = explode(',', $oldkey);

                    ArticleModel::where('id', $id)->update($params);

                    // $newkeys = explode(',', $params['keywords']);

                    // $keys = array_diff($newkeys, $oldkeys);

                    // $dec  = array_diff($oldkeys, $newkeys);

                    // TagModel::saveTags($keys, (int) $id);

                    // TagModel::decNums($dec, (int) $id);   
                } else {
                    $params['userid'] = $this->getSysUser()->userid;
                    $params['username'] = $this->getSysUser()->username;
                    $params['status'] = 1;  // 后台文章默认审核
                    
                    $data = ArticleModel::create($params);

                    // $keys = explode(',', $params['keywords']);
                    
                    // TagModel::saveTags($keys, (int) $data->id, (int) $data->cid);
                }
                return ReUtils::success();
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                return ReUtils::error("错误提示:" . $msg);
            }
        } else {
            if ($id) {
                $data = ArticleModel::find($id);
                $cid = $data->cid;
            } else {
                $data = new ArticleModel();
                $data->cid = $cid;
                $data->pubtime = date("Y-m-d H:i:s", time());
            }

            $category = CategoryModel::info(intval($cid));

            View::assign('category', $category);
            View::assign('data', $data);
            View::assign('cid', $cid);

            return View::fetch();
        }
    }
}
